package com.jackrain.nea.common;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;

@Service
public class PostQueryEvemt {


    @Autowired
    private PropertiesConf pconf;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    private RedisObjectSerializer serializer = new RedisObjectSerializer();

    /**
     * CP跨库查询
     */
    public JSONObject queryCP(String tablename, String fileds, JSONObject wherejo) {

        HashMap hashMap = new HashMap();
        hashMap.put("tableName", tablename);
        hashMap.put("fields", fileds);
        hashMap.put("whereKeys", wherejo);
        hashMap.put("type", HashMap.class);
        String erpUrl = pconf.getProperty("fc.edas.url", "");
        URI uri = null;
        try {
            uri = new URI(erpUrl + "/fc/api/query");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        JSONObject paramObject = new JSONObject();
        paramObject.put("address", "com.jackrain.nea.web.QueryCmd");
        paramObject.put("group", "cp");
        paramObject.put("version", "2");
        byte[] querymap = serializer.serialize(hashMap);
        try {
            paramObject.put("map", new String(querymap, "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        map.add("param", paramObject.toJSONString());
        String retStr = restTemplateUtil.doPostForm(uri, map);
        JSONObject selectMenuType = JSON.parseObject(retStr).getJSONObject("data");

        return selectMenuType;
    }

}